package com.atguigu.gmall.list.service.impl;

import com.atguigu.gmall.list.service.SearchHistoryObserver;
import com.atguigu.gmall.list.service.SearchHistorySubject;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class UserSearchHistory implements SearchHistorySubject {
    private List<SearchHistoryObserver> observers = new ArrayList<>();

    @Override
    public void addObserver(SearchHistoryObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(SearchHistoryObserver observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers(String userId, String keyword) {
        for (SearchHistoryObserver observer : observers) {
            observer.updateSearchHistory(userId, keyword);
        }
    }

    public void userSearched(String userId, String keyword) {
        // 用户搜索逻辑
        // ...
        // 通知所有观察者
        notifyObservers(userId, keyword);
    }
}
